﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _09_Tetris
{
    internal class Map
    {
        public enum Type { None, Wall, Block }

        int width;
        int height;
        int[,] data;

        public Map()
        {
            width = 12;
            height = 22;
            data = new int[height, width];
        }

        public void Init()
        {
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (i == 0 || j == 0 || i == height - 1 || j == width - 1)
                    {
                        data[i, j] = (int)Type.Wall;
                    }
                }
            }
        }

        public void Render()
        {
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (data[i, j] == (int)Type.Wall)
                    {
                        Console.SetCursorPosition(j * 2, i);
                        Console.WriteLine("■");
                    }
                }
            }
        }

        public Type GetType(Pos pos, int offsetX, int offsetY)
        {
            return (Type)data[pos.y + offsetY, pos.x + offsetX];
        }

        public void ChangeType(Pos pos,Type type)
        {
            data[pos.y,pos.x] = (int)type;
        }

        public int ClearLine()
        {
            int line = 0, count = 0,index = 20;
            for(int i = height - 2;i > 0; i--)
            {
                count = 0;
                for (int j = 1; j < width - 1; j++)
                {
                    // 边记录当前行方块数量
                    if (data[i, j] == (int)Type.Wall)
                    {
                        count++;
                    }
                    // 边复制
                    data[index, j] = data[i, j];
                }
                // 不是满行 index往上挪
                if(count < 10)
                {
                    index--;
                }
                else
                {
                    line++;
                }
            }
            Console.Clear();
            return line;
        }
    }
}
